#include<bits/stdc++.h>
using namespace std;

int n, m;
vector<int> v[100010];
int d[100010];
int st[100010];
int bfs(){
    queue<int> q;
    q.push(1);
    st[1] = true;
    d[1] = 0;
    while(!q.empty()){
        int t = q.front();
        if (t == n) return d[t];
        q.pop();
        for (auto c : v[t]){
            if (!st[c]){
                d[c] = d[t] + 1;
                st[c] = true;
                q.push(c);
            }
        }
    }
    return -1;
}   
int main(){
    cin >> n >> m;
    for (int i = 1; i <= m; i++){
        int a, b;
        cin >> a >> b;
        v[a].push_back(b);
    }
    cout << bfs() << endl;

}